package org.duiduo.controller;

import org.duiduo.common.bean.UserAddressVO;
import org.duiduo.common.bean.UserVO;
import org.duiduo.common.request.UserFindPageRequest;
import org.duiduo.common.request.UserRegisterRequest;
import org.duiduo.common.response.Page;
import org.duiduo.common.response.Result;
import org.duiduo.common.valid.ValidationGroups;
import org.duiduo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 用户控制层
 *
 * @author liangze
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    @Autowired
    private BCryptPasswordEncoder cryptPasswordEncoder;

    /**
     * 统计数据
     */
    @GetMapping("/count")
    public Result<List<UserAddressVO>> count() {
        return Result.success(userService.countAddress());
    }

    /**
     * 根据用户ID获取对应用户
     */
    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id) {
        return Result.success(userService.getById(id));
    }

    /**
     * 查询所有的用户 - 如果不是管理员只查询登录用户信息
     */
    @GetMapping("/findAll")
    public Result<?> findAll() {
        return Result.success(userService.findAll(getUser()));
    }

    /**
     * 登录
     */
    /*@PostMapping("/login")
    public Result login(@RequestBody @Valid UserLoginRequest request) {
        return Result.success(userService.login(request));
    }*/

    /**
     * 注册
     */
    @PostMapping("/register")
    public Result register(@RequestBody @Valid UserRegisterRequest request) {
        return Result.success(userService.register(request));
    }

    /**
     * 新增
     */
    @PostMapping
    public void save(@RequestBody @Validated(ValidationGroups.Insert.class) UserVO userVO) {
        userService.save(userVO);
    }

    /**
     * 编辑修改
     */
    @PutMapping
    public void update(@RequestBody @Valid @Validated(ValidationGroups.Update.class) UserVO userVO) {
        userService.update(userVO);
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        userService.delete(id);
    }

    /**
     * 分页查询
     */
    @PostMapping("/query")
    public Result<Page<UserVO>> findPage(@RequestBody UserFindPageRequest request) {
        return Result.success(userService.findPage(request));
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        userService.export(response);
    }

    /**
     * 导入
     */
    @PostMapping("/import")
    public Result<?> upload(MultipartFile file) {
        return userService.upload(file);
    }
}
